<?php
session_save_path('sessions');
session_start();

include("include/dbUserTypes.php");

// Include our user class
include_once("include/user.php");

// Create a variable to store our user if we're validated
$user = null;

// Check if we're in a valid session. If not, go back to main login page.

if( !isset($_SESSION['user']) || $_SESSION['user'] == null ) {
    header("location:main_login.php");
}
else {
    // Unserialize our user to turn it back into a useful object
    $user = unserialize($_SESSION['user']);
}

// If we're not a registered (or admin) go back to the user redirection page
if( $user->getUserType() != $USERTYPE_REGISTERED && $user->getUserType() != $USERTYPE_ADMIN)
    header("location:login_success.php");



// Include the header (CSS files, etc)
include("include/dbconnection.php");
include("include/header.php"); ?>

 
<body>
  <div id="wrapper">
    <div id="header"> </div>
    <div id="left">
      <div id="logo">
        <h1>Home Sale</h1>
        <p>Only By Owner</p>
      </div>
      <div id="nav">
        <ul>
          <li class="important">
            <a href="addAhomeForSale.php">Add a House for sale</a>
          </li>
          <li>
            <a href="updateAhome.php">Update House information</a>
          </li>
          <li >
            <a href="updateAhomePicture.php">Update House picture</a>
          </li>
          <li>
            <a href="searchhouse.php">Search House</a>
          </li>
          <li>
            <a href="emailMenu.php">Mailbox</a>
          </li>
          <li>
            <a href="logout.php">Logout</a>
          </li>
        </ul>
      </div>
      <div id="news">
        <?php include("adgeneratorleft1.php");?>
        <div class="hr-dots"> </div>
        <?php include("adgeneratorleft2.php");?>
        <p class="more"><a href="registered_index.php">more</a></p>
      </div>
      <div id="support">
      </div>
    </div>
    <div id="right">
      <h2>New House added</h2>
      <div id="welcome">
        <p>You can modify the information if you made a mistake.</p>

		<?php

			$address = mysql_real_escape_string($_POST["address"]);
			$streetName = mysql_real_escape_string($_POST["streetName"]);
			$city = mysql_real_escape_string($_POST["city"]);
			$country = mysql_real_escape_string($_POST["country"]);
			$district = mysql_real_escape_string($_POST["district"]);
			$zone = mysql_real_escape_string($_POST["zone"]);
			$postalCode = mysql_real_escape_string($_POST["postalCode"]);
			$lotSize = mysql_real_escape_string($_POST["lotSize"]);
			$propertyType = mysql_real_escape_string($_POST["propertyType"]);
			$yearBuilt = mysql_real_escape_string($_POST["yearBuilt"]);
			$heating = mysql_real_escape_string($_POST["heating"]);
			$cooling = mysql_real_escape_string($_POST["cooling"]);
			$parking = mysql_real_escape_string($_POST["parking"]);
			$garage = mysql_real_escape_string($_POST["garage"]);
			$driveway = mysql_real_escape_string($_POST["driveway"]);
			$neighbourhood = mysql_real_escape_string($_POST["neighbourhood"]);
			$recentImprovements = mysql_real_escape_string($_POST["recentImprovements"]);
			$evaluation = mysql_real_escape_string($_POST["evaluation"]);
			$taxes = mysql_real_escape_string($_POST["taxes"]);
			$utilityCost = mysql_real_escape_string($_POST["utilityCost"]);
			$price = mysql_real_escape_string($_POST["price"]);
			$comments = mysql_real_escape_string($_POST["comments"]);
			$listingDate = date("U");
			$currentUser = $user->getUserID();
			
			$sql="INSERT INTO home (address, streetName, city, country, district, zone,postalCode,
									lotSize, propertyType, yearBuilt,heating, cooling, parking, garage,
									driveway, neighbourhood, recentImprovements, evaluation,taxes,
									utilityCost, price, comments,listingDate)
			VALUES
									('$address', '$streetName', '$city', '$country', '$district', '$zone', '$postalCode',
									'$lotSize', '$propertyType', '$yearBuilt','$heating', '$cooling', '$parking', '$garage',
									'$driveway', '$neighbourhood', '$recentImprovements', '$evaluation','$taxes',
									'$utilityCost', '$price', '$comments', '$listingDate')";

			if (!mysql_query($sql))
			  {
			  die('Error: ' . mysql_error());
			  }

			$homeIDquery = mysql_query("select * from home where address = $address AND streetName = '$streetName' AND city = '$city' AND country = '$country'");
 
			while($row1 = mysql_fetch_array($homeIDquery)){
			$homeID = $row1[$TABLE_HOME_HOMEID];
			}


			$sql2="INSERT INTO list(userID, homeID) values($currentUser, $homeID)";

			if (!mysql_query($sql2))
			  {
			  die('Error: ' . mysql_error());
			  }
		
			$result = mysql_query("select * from home where homeID = $homeID");
			
				$row = mysql_fetch_array($result);
        
        ?>



        <table>
		<form name="myForm" action="updateAhomeValidate.php" method="post" onsubmit="return validate()">
	<tr><td>Address:</td><td><input type="text" name="address" value = <?php echo $row[$TABLE_HOME_ADDRESS]?>></td></tr>
	<tr><td>Street Name:</td><td><input type="text" name="streetName" value = "<?php echo $row[$TABLE_HOME_STREETNAME]?>"></td></tr>
	<tr><td>City:</td><td><input type="text" name="city" value = "<?php echo $row[$TABLE_HOME_CITY]?>"></td></tr>
	<tr><td>Country:</td><td><input type="text" name="country" value = "<?php echo $row[$TABLE_HOME_COUNTRY]?>"></td></tr>
	<tr><td>District:</td><td><input type="text" name="district" value = "<?php echo $row[$TABLE_HOME_DISTRICT]?>"></td></tr>
	<tr><td>Zone:</td><td><input type="text" name="zone" value = "<?php echo $row[$TABLE_HOME_ZONE]?>"></td></tr>
	<tr><td>Postal Code:</td><td><input type="text" name="postalCode" value = "<?php echo $row[$TABLE_HOME_POSTALCODE]?>"></td></tr>
	<tr><td>Size of lot:</td><td><input type="text" name="lotSize" value = <?php echo $row[$TABLE_HOME_LOTSIZE]?>></td></tr>

		<tr><td>Type of property:</td><td>

	 <select name="propertyType">
		<option value="<?php echo $row[$TABLE_HOME_PROPERTYTYPE]?>">

        <?php 
		switch($row[$TABLE_HOME_PROPERTYTYPE]){
        case 1:
        {
          echo "Condo";
          break;
        }
        case 2:
        {
          echo "Apartment";
          break;
        }
        case 3:
        {
          echo "Single family";
          break;
        }
        case 4:
        {
          echo "Cottages";
          break;
        }
		}
        ?></option>
                    <option value="1">Change to : Condo</option>
                    <option value="2">Change to : Apartment</option>
                    <option value="3">Change to : Single family</option>
                    <option value="4">Change to : Cottage</option> 
                  </select></td></tr>




	<tr><td>Year built:</td><td><input type="text" name="yearBuilt" value = <?php echo $row[$TABLE_HOME_YEARBUILT]?>></td></tr>
	<tr><td>Heating</td></tr>
	<tr><td><input type="radio" name="heating" value="1" 
	
	<?php
		if ($row[$TABLE_HOME_HEATING] == 1)
			echo 'checked';?>
	> Yes</td><td><input type="radio" name="heating" value="0" 
	
	<?php
		if ($row[$TABLE_HOME_HEATING] == 0)
			echo 'checked';?>
	>No</td></tr>
	<tr><td>Cooling</td></tr>
		<tr><td><input type="radio" name="cooling" value="1" 
	
	<?php
		if ($row[$TABLE_HOME_COOLING] == 1)
			echo 'checked';?>
	> Yes</td><td><input type="radio" name="cooling" value="0" 
	
	<?php
		if ($row[$TABLE_HOME_COOLING] == 0)
			echo 'checked';?>
	>No</td></tr>
	<tr><td>Parking</td</tr>
			<tr><td><input type="radio" name="parking" value="1" 
	
	<?php
		if ($row[$TABLE_HOME_PARKING] == 1)
			echo 'checked';?>
	> Yes</td><td><input type="radio" name="parking" value="0" 
	
	<?php
		if ($row[$TABLE_HOME_PARKING] == 0)
			echo 'checked';?>
	>No</td></tr>
	<tr><td>Garage</td></tr>
		<tr><td><input type="radio" name="garage" value="1" 
	
	<?php
		if ($row[$TABLE_HOME_GARAGE] == 1)
			echo 'checked';?>
	> Yes</td><td><input type="radio" name="garage" value="0" 
	
	<?php
		if ($row[$TABLE_HOME_GARAGE] == 0)
			echo 'checked';?>
	>No</td></tr>
	<tr><td>Driveway</td></tr>
			<tr><td><input type="radio" name="driveway" value="1" 
	
	<?php
		if ($row[$TABLE_HOME_DRIVEWAY] == 1)
			echo 'checked';?>
	> Yes</td><td><input type="radio" name="driveway" value="0" 
	
	<?php
		if ($row[$TABLE_HOME_DRIVEWAY] == 0)
			echo 'checked';?>
	>No</td></tr>
	<tr><td>Neighbourhood:</td><td><input type="text" name="neighbourhood" value = "<?php echo $row[$TABLE_HOME_NEIGHBOURHOOD]?>"></td></tr>
	<tr><td>Recent Improvements:</td><td><input type="text" name="recentImprovements" value = "<?php echo $row[$TABLE_HOME_IMPROVEMENTS]?>"></td></tr>
	<tr><td>Evaluated Price:</td><td><input type="text" name="evaluation" value = <?php echo $row[$TABLE_HOME_EVALUATION]?>></td></tr>
	<tr><td>Property Taxes:</td><td><input type="text" name="taxes" value = <?php echo $row[$TABLE_HOME_TAXES]?>></td></tr>
	<tr><td>Utility Cost:</td><td><input type="text" name="utilityCost" value = <?php echo $row[$TABLE_HOME_UTILITYCOST]?>></td></tr>
	<tr><td>Price:</td><td><input type="text" name="price" value = <?php echo $row[$TABLE_HOME_PRICE]?>></td></tr>
	
			<tr>
				<td>Comments:</td>
				<td>
					<textarea rows="5" cols="20" name="comments" wrap="physical" style="width:327px; height:129px;">
						<?php echo $row[$TABLE_HOME_COMMENTS]?>
					</textarea>
				</td>
			</tr>
			
	<tr><td>Listing Date:</td><td><?php echo date("d/m/Y", $row[$TABLE_HOME_LISTINGDATE])?></td></tr>
	<input type="hidden" name="homeID" value = <?php echo $row[$TABLE_HOME_HOMEID]?>>
	</table>
	<br>
    <input type="submit" value="Update" />
    </form>	
				<?php

				mysql_close();
				
				?>
				</table>


		<SCRIPT>


			function is_int(input)
			{
			return !isNaN(input)&&parseInt(input)==input;
			}

			function validate()
			{
			mNv=myForm.address.value;
			if (mNv=='')
			{
			alert('The address of the house is a required field.');
			return false;
			}
			if (!is_int(mNv))
			{
			alert('The Address has to be a number.');
			return false;
			}
			mNv=myForm.streetName.value;
			if (mNv=='')
			{
			alert('The Street Name is a required field.');
			return false;
			}
			mNv=myForm.city.value;
			if (mNv=='')
			{
			alert('The City in which the house is a required field.');
			return false;
			}
			mNv=myForm.country.value;
			if (mNv=='')
			{
			alert('The Country in which the house is a required field.');
			return false;
			}
			mNv=myForm.district.value;
			if (mNv=='')
			{
			alert('The District in which the house is a required field.');
			return false;
			}
			mNv=myForm.zone.value;
			if (mNv=='')
			{
			alert('The zone in which the house is a required field.');
			return false;
			}
			mNv=myForm.postalCode.value;
			if (mNv=='')
			{
			alert('The Postal Code the house is a required field.');
			return false;
			}
			mNv=myForm.lotSize.value;
			if (mNv=='')
			{
			alert('The Size of lot is a required field.');
			return false;
			}
			if (!is_int(mNv))
			{
			alert('The Size of lot has to be a number.');
			return false;
			}
			mNv=myForm.propertyType.value;
			if (mNv=='')
			{
			alert('The Type of property is a required field.');
			return false;
			}
			if (!is_int(mNv))
			{
			alert('The Type of roperty has to be a number.');
			return false;
			}
			mNv=myForm.yearBuilt.value;
			if (mNv=='')
			{
			alert('The Year built is a required field.');
			return false;
			}
			if (!is_int(mNv))
			{
			alert('The Year built has to be a number.');
			return false;
			}
			if (!(myForm.heating[0].checked || myForm.heating[1].checked)) {
			alert('You have to specify if there is heating.');
			return false;
			}
			if (!(myForm.cooling[0].checked || myForm.cooling[1].checked)) {
			alert('You have to specify if there is cooling.');
			return false;
			}
			if (!(myForm.parking[0].checked || myForm.parking[1].checked)) {
			alert('You have to specify if there is a parking.');
			return false;
			}
			if (!(myForm.garage[0].checked || myForm.garage[1].checked)) {
			alert('You have to specify if there is a garage.');
			return false;
			}
			if (!(myForm.driveway[0].checked || myForm.driveway[1].checked)) {
			alert('You have to specify if there is a driveway.');
			return false;
			}
			mNv=myForm.neighbourhood.value;
			if (mNv=='')
			{
			alert('You have tell in which neighbourhood this house is.');
			return false;
			}
			mNv=myForm.evaluation.value;
			if (mNv=='')
			{
			alert('You have to give the Evaluated price');
			return false;
			}if (!is_int(mNv))
			{
			alert('The Evaluated price has to be a number.');
			return false;
			}
			mNv=myForm.taxes.value;
			if (mNv=='')
			{
			alert('You have to specify the Property Taxes.');
			return false;
			}
			if (!is_int(mNv))
			{
			alert('The Property Taxes has to be a number.');
			return false;
			}
			mNv=myForm.utilityCost.value;
			if (mNv=='')
			{
			alert('You have to specify the Utility cost.');
			return false;
			}
			if (!is_int(mNv))
			{
			alert('The Utility cost has to be a number.');
			return false;
			}
			mNv=myForm.price.value;
			if (mNv=='')
			{
			alert('You have to specify the the house Price.');
			return false;
			}
			if (!is_int(mNv))
			{
			alert('The house Price has to be a number.');
			return false;
			}
			else
			return true;
			}
		</SCRIPT>
		
      </div>
      <h3></h3>
      <div id="profile">
        <div id="corp">
          <?php include("adgeneratorbottomleft.php");?>
        </div>
        <div id="indu">
          <?php include("adgeneratorbottomright.php");?>
        </div>
      </div>
    </div>
    <div class="clear"> </div>
    <div id="spacer"> </div>
    <div id="footer">
      <div id="copyright">
        &copy; Template taken from http://www.freewebsitetemplates.com/
      </div>
	  <div id="footerline"></div>
    </div>
	
  </div>
</body>